package cn.dipper.mongo.tiny_mongo;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.SAXOutputter;
import org.jdom.output.XMLOutputter;

import com.google.gson.Gson;

public class TinyMogo {

	private static final String  _UUID = "_uuid";
	
	
	/** 
	* @Title: createDatabase 
	* @Description: 创建数据库
	* @param @param filePath
	* @param @param tablename
	* @param @return
	* @param @throws IOException    设定文件 
	* @return boolean    返回类型 
	* @throws 
	*/
	public boolean createDatabase(String filePath,String tablename) throws IOException{
		boolean flag = false;
		FileOutputStream fileout = new FileOutputStream(filePath);
		Document document = new Document();
		Element root = new Element("database");
		
		Element table1 = new Element("table");
		table1.setAttribute("name", tablename);
		
		root.addContent(table1);
		
		document.addContent(root);
		
		XMLOutputter out = new XMLOutputter();
		out.output(document, fileout);
		fileout.close();
		flag = true;
		System.out.println("数据库创建成功");
		return flag;
	}
	
	/** 
	* @Title: insertData 
	* @Description: 插入数据
	* @param @param filePath
	* @param @param tablename
	* @param @param ob
	* @param @return
	* @param @throws JDOMException
	* @param @throws IOException    设定文件 
	* @return boolean    返回类型 
	* @throws 
	*/
	public boolean insertData(String filePath,String tablename,Object ob) throws JDOMException, IOException{
		boolean flag = false;
		Gson gson = new Gson();
		FileInputStream inputs = new FileInputStream(filePath);
		SAXBuilder sax = new SAXBuilder();
		
		Document document = sax.build(inputs);
		
		Element root = document.getRootElement();
		List<Element> tables =  root.getChildren();
		for(Element table:tables){
			String itemname = table.getAttributeValue("name");
			if(itemname.equals(tablename)){
				String jsonstr = gson.toJson(ob);
				Map map = gson.fromJson(jsonstr, Map.class);
				map.put(_UUID, UUID.randomUUID().toString());
				Element data = new Element("item");
				data.addContent(gson.toJson(map));
				table.addContent(data);
				flag = true;
				break;
				
			}
		}
		
		XMLOutputter out = new XMLOutputter();
		
		FileOutputStream output = new FileOutputStream(filePath);
		
		out.output(document, output);
		output.close();
		if(flag){
			System.out.println("插入成功");
		}
		return flag;
	}
	
	/** 
	* @Title: updateData 
	* @Description: 修改数据
	* @param @param filePath
	* @param @param tablename
	* @param @param ob
	* @param @return
	* @param @throws JDOMException
	* @param @throws IOException    设定文件 
	* @return boolean    返回类型 
	* @throws 
	*/
	public boolean updateData(String filePath,String tablename,Object ob) throws JDOMException, IOException{
		boolean flag = false;
		Gson gson = new Gson();
		FileInputStream inputs = new FileInputStream(filePath);
		
		SAXBuilder sax = new SAXBuilder();
		
		Document document = sax.build(inputs);
		
		Element root = document.getRootElement();
		
		List<Element> tables = root.getChildren();
		for(Element table:tables){
			String name = table.getAttributeValue("name");
			if(name.equals(tablename)){
				List<Element> items =  table.getChildren();
				for(Element item:items){
					Map oldmp = gson.fromJson(item.getText(), Map.class);
					Map newmp = gson.fromJson(gson.toJson(ob), Map.class);
					if(oldmp.get(_UUID).equals(newmp.get(_UUID))){
						oldmp.putAll(newmp);
						Element newitem = new Element("item");
						newitem.addContent(gson.toJson(oldmp));
						table.removeContent(item);
						table.addContent(newitem);
						flag = true;
						break;
					}
						
				}
			}
		}
		
		inputs.close();
		XMLOutputter out = new XMLOutputter();
		FileOutputStream fileout = new FileOutputStream(filePath);
		out.output(document,fileout);
		fileout.close();
		if(flag){
			System.out.println("修改成功");
		}
		return flag;
	}
	
	
	public boolean deleteData(String filePath,String tablename,Object ob) throws JDOMException, IOException{
		boolean flag = false;
		Gson gson = new Gson();
		FileInputStream inputS = new FileInputStream(filePath);
		SAXBuilder sax = new SAXBuilder();
		Document document = sax.build(inputS);
		Element root = document.getRootElement();
		List<Element> tables = root.getChildren();
		for(Element table :tables){
			String name = table.getAttributeValue("name");
			if(name.equals(tablename)){
				List<Element> items =  table.getChildren();
				for(Element item:items){
					Map oldmp = gson.fromJson(item.getText(), Map.class);
					Map newmp = gson.fromJson(gson.toJson(ob), Map.class);
					if(oldmp.get(_UUID).equals(newmp.get(_UUID))){
						table.removeContent(item);
						flag = true;
						break;
					}
				}
					
			}
		}
		
		inputS.close();
			XMLOutputter out = new XMLOutputter();
			FileOutputStream output = new FileOutputStream(filePath);
			out.output(document, output);
			output.close();
			if(flag){
				System.out.println("删除成功");
			}
		return flag;
	}
	
	public List<Map> selectAll(String filePath,String tablename) throws JDOMException, IOException{
		List<Map> listmap = new ArrayList();
		Gson gson = new Gson();
		FileInputStream input = new FileInputStream(filePath);
		
		SAXBuilder sax = new SAXBuilder();
		Document document= sax.build(input);
		
		Element root = document.getRootElement();
		
		List<Element> tables = root.getChildren();
		for(Element table:tables){
			String name = table.getAttributeValue("name");
			if(name.equals(tablename)){
				List<Element> items = table.getChildren();
				for(Element item :items){
					String jsonstr = item.getText();
					if(jsonstr==null){
						break;
					}
				   Map map = gson.fromJson(item.getText(),Map.class);
				   listmap.add(map);
				}
			}
		}
		input.close();
		
		return listmap;
	}
	
}
